﻿/*
 * Created by SharpDevelop.
 * User: Home
 * Date: 24/06/2008
 * Time: 8:18
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using csharp.rad.general;
using csharp.rad.sisponto.kernel;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.sisponto
{
	/// <summary>
	/// Description of frmImportarFuncionariosRelogioHenry.
	/// </summary>
	public partial class frmImportarFuncionariosRelogioHenry : KryptonForm
	{
		private Database bd;
		
		public frmImportarFuncionariosRelogioHenry(Database bd)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.bd = bd;
		}
		

		
		private void BtnBuscarFuncsClick(object sender, EventArgs e)
		{
			Database database = new Database();
			String matricula, nome, _sql;
			
			Database.Result result = new Database.Result();
			database.Protocol = "Firebird";
			database.Host= txtHost.Text;
			database.Name = txtBdName.Text;
			database.User = txtUser.Text;
			database.Password = txtPassword.Text;
			
			result = database.Connect();
			if( result.Status == Database.Status.Failed )
			{
				KryptonMessageBox.Show("Erro: "+result.ErrorMessage);
				return;
			}
			
			_sql = "SELECT * FROM HE02";
			System.Data.DataSet dsFuncs = new System.Data.DataSet();
			dsFuncs = database.ExecuteQuery(_sql,"HE02",out result);
			
			if( result.Status == Database.Status.Failed )
			{
				KryptonMessageBox.Show("Erro: "+result.ErrorMessage);
				btnImportar.Enabled = false;
				return;
			}
			
			dgvFuncs.Rows.Clear();
			foreach ( System.Data.DataRow linha in dsFuncs.Tables[0].Rows )
			{
				matricula = linha["HE02_ST_MATRICULA"].ToString();
				nome      = linha["HE02_ST_NOME"].ToString();
				
				dgvFuncs.Rows.Add(new String[]{"true",nome,matricula});
			}
			btnImportar.Enabled = true;
		}
		
		private void importFuncs()
		{
			Funcionario func = new Funcionario(bd);
			
			bd.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
			
			for(int i = 0; i < dgvFuncs.Rows.Count; i++)
			{
				if ( Convert.ToBoolean(dgvFuncs.Rows[i].Cells["importar"].Value.ToString()) )
				{
					func.codigo             		= kernel.Functions.makeStringEsquerda(Convert.ToString(dgvFuncs.Rows[i].Cells["matricula"].Value),20,'0');
					if( func.getIdByCodigo(func.codigo) > 0 )
					{
						bd.RollBackTransaction();
						KryptonMessageBox.Show("Já existe um funcinário com o código '"+func.codigo+"' cadastrado!");
						return;
					}
					
					func.nome       					= Convert.ToString(dgvFuncs.Rows[i].Cells["nome"].Value);
					func.endereco   					= "Ruas das mariquinhas";
					func.bairro     					= "Frutal";
					func.cidade     					= "Casados";
					func.estado     					= "GO";
					func.cep        					= "42-4242";
					func.fone       					= "(042)4242-4242";
					func.celular   					= "(042)4242-4242";
					func.email      					= "miguxo@orkut.com";
					func.cargo      		   		= "psicologo";
					func.setor      		   		= "RH";
					func.data_nasc          		= Convert.ToDateTime("25/09/1942");
					func.data_admissao      		= DateTime.Now;
					func.estado_civil  				= "CASADO(A)";
					func.sexo 							= "F";
					func.num_carteira 				= "4242";
					func.cpf                		= kernel.Functions.makeStringEsquerda(func.codigo,11,'0');
					func.rg                 		= "42";
					func.e_professor        		= false;
					func.salario            		= 0.42;
					func.ativo              		= true;
					func.num_conta          		= "424242";
					func.permite_ponto_codigo 		= false;
					func.barrar_fora_expediente 	= true;
					func.barrar_fora_horario 		= true;
					func.id_banco 						= 1;
					func.id_horario_func 			= 1;
					
					func.data_cadastro = DateTime.Now;
					if (func.inserir(false) < 1)
					{
						bd.RollBackTransaction();
						KryptonMessageBox.Show("Erro: \n" + func._msg_erro);
						return;
					}
				}
			}
			KryptonMessageBox.Show("Registros salvos com sucesso!");
			btnImportar.Enabled = false;
			bd.CommitTransaction();
		}
		
		private void BtnImportarClick(object sender, EventArgs e)
		{
			importFuncs();
		}
		
		private void BtnCancelarClick(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
